//
//  YCLNewFeatureCell.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/12.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

class YCLNewFeatureCell: UICollectionViewCell {
    
    //定义一个索引
    var currentIndex:Int = 0 {
        
        didSet{
        
            let nameString:String = "new_feature_\(currentIndex + 1)"
            
            bgView.image = UIImage.init(named:nameString)
            
            //设置按钮状态（通过比较index的值来确定按钮状态）
            startBtn.hidden = true
        }
        

    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //设置UI
        prepareUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI() {
    
        //添加子控件
        contentView.addSubview(bgView)
        contentView.addSubview(startBtn)
        
//        bgView.translatesAutoresizingMaskIntoConstraints = false
//        startBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //设置约束
        bgView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        startBtn.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
        
    }
    
    //开始动画
    func startAnimation() {
        
        startBtn.hidden = false
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        //按钮出现
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: {
            self.startBtn.transform = CGAffineTransformIdentity
            }, completion: { (_) in
                
//                YCLPrint("动画完成了")
        })
    }
    
    //监听按钮点击事件
    @objc private func startBtnDidClick() {
    
        //跳到微博信息界面控制器
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(YCLTabBarController())
    }
    
    
    //MARK 懒加载
    
    private lazy var bgView:UIImageView = UIImageView.init(image: UIImage.init(named: "new_feature_1"))
    
    //开始按钮
    private lazy var startBtn:UIButton = {
    
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage.init(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage.init(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        
        //添加按钮点击事件
        btn.addTarget(self, action: Selector("startBtnDidClick"), forControlEvents: UIControlEvents.TouchUpInside)

        btn.sizeToFit()
        
        return btn
    }()
    
}
